<template>
  <div class="mod-crm__crmcustomer">
    <el-form :inline="true" :model="state.dataForm" @keyup.enter="state.getDataList()">
      <!-- <el-form-item>
        <el-input v-model="state.dataForm.id" placeholder="ID" clearable></el-input>
      </el-form-item> -->
      <el-form-item>
        <el-input v-model="state.dataForm.cusName" placeholder="客户名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="state.dataForm.areaCode" placeholder="电话区号" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="state.dataForm.mobile" placeholder="手机号" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="state.dataForm.email" placeholder="邮箱地址" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="state.dataForm.headUid" placeholder="负责人" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="state.dataForm.gender" placeholder="性别" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="state.dataForm.type" placeholder="客户类型;个人；公司" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="state.dataForm.channel" placeholder="渠道来源;whatsapp；weixin；h5；Instagram；kwai；dianpin" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="state.dataForm.authChannels" placeholder="认证渠道;认证渠道拼接，" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="state.dataForm.orderStatus" placeholder="客户状态;生效；黑名单；" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="state.dataForm.remark" placeholder="描述" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="state.dataForm.status" placeholder="状态" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="state.getDataList()">{{ $t("query") }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="info" @click="state.exportHandle()">{{ $t("export") }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="state.hasPermission('crm:crmcustomer:save')" type="primary" @click="addOrUpdateHandle()">{{ $t("add") }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="state.hasPermission('crm:crmcustomer:delete')" type="danger" @click="state.deleteHandle()">{{ $t("deleteBatch") }}</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="state.dataListLoading" :data="state.dataList" border @selection-change="state.dataListSelectionChangeHandle" style="width: 100%">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <!-- <el-table-column prop="id" label="ID" header-align="center" align="center"></el-table-column> -->
      <el-table-column prop="cusName" label="客户名称" header-align="center" align="center"></el-table-column>
      <el-table-column prop="areaCode" label="电话区号" header-align="center" align="center"></el-table-column>
      <el-table-column prop="mobile" label="手机号" header-align="center" align="center"></el-table-column>
      <el-table-column prop="email" label="邮箱地址" header-align="center" align="center"></el-table-column>
      <el-table-column prop="headUid" label="负责人" header-align="center" align="center"></el-table-column>
      <el-table-column prop="gender" label="性别" header-align="center" align="center"></el-table-column>
      <el-table-column prop="type" label="客户类型" header-align="center" align="center"></el-table-column>
      <el-table-column prop="channel" label="渠道来源" header-align="center" align="center"></el-table-column>
      <el-table-column prop="authChannels" label="认证渠道" header-align="center" align="center"></el-table-column>
      <el-table-column prop="orderStatus" label="客户状态" header-align="center" align="center"></el-table-column>
      <el-table-column prop="remark" label="描述" header-align="center" align="center"></el-table-column>
      <el-table-column prop="status" label="状态" header-align="center" align="center">
        <template v-slot="scope">
          <el-tag v-if="scope.row.status === 0" size="small" type="danger">{{ $t("user.status0") }}</el-tag>
          <el-tag v-else size="small" type="success">{{ $t("user.status1") }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="num" label="排序" header-align="center" align="center"></el-table-column>
      <!-- <el-table-column prop="creator" label="创建人" header-align="center" align="center"></el-table-column>
      <el-table-column prop="createDate" label="创建时间" header-align="center" align="center"></el-table-column>
      <el-table-column prop="updater" label="更新人" header-align="center" align="center"></el-table-column>
      <el-table-column prop="updateDate" label="更新时间" header-align="center" align="center"></el-table-column> -->
      <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
        <template v-slot="scope">
          <el-button v-if="state.hasPermission('crm:crmcustomer:update')" type="primary" link @click="addOrUpdateHandle(scope.row.id)">{{ $t("update") }}</el-button>
          <el-button v-if="state.hasPermission('crm:crmcustomer:delete')" type="primary" link @click="state.deleteHandle(scope.row.id)">{{ $t("delete") }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :current-page="state.page" :page-sizes="[10, 20, 50, 100]" :page-size="state.limit" :total="state.total" layout="total, sizes, prev, pager, next, jumper" @size-change="state.pageSizeChangeHandle" @current-change="state.pageCurrentChangeHandle"> </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update :key="addKey" ref="addOrUpdateRef" @refreshDataList="state.getDataList"></add-or-update>
  </div>
</template>

<script lang="ts" setup>
import useView from "@/hooks/useView";
import { nextTick, reactive, ref, toRefs, watch } from "vue";
import AddOrUpdate from "./crmcustomer-add-or-update.vue";

const view = reactive({
  getDataListURL: "/crm/crmcustomer/page",
  getDataListIsPage: true,
  exportURL: "/crm/crmcustomer/export",
  deleteURL: "/crm/crmcustomer",
  deleteIsBatch: true,
  dataForm: {
    id: "",
    cusName: "",
    areaCode: "",
    mobile: "",
    email: "",
    headUid: "",
    gender: "",
    type: "",
    channel: "",
    authChannels: "",
    orderStatus: "",
    remark: "",
    status: "",
  }
});

const state = reactive({ ...useView(view), ...toRefs(view) });


const addKey = ref(0);
const addOrUpdateRef = ref();
const addOrUpdateHandle = (id?: number) => {
  addKey.value++;
  nextTick(() => {
    addOrUpdateRef.value.init(id);
  });
};
</script>